﻿using System.Collections.Generic;
using System.Linq;
using yiCommerce.Core.Data;
using yiCommerce.Core.Domain.Customers;
using yiCommerce.Core.Domain.FriendShips;
using yiCommerce.Service.Customers;

namespace yiCommerce.Service.FriendShips
{
    public class FriendShipService:IFriendShipService
    {

        private IRepository<FriendShip> _friendShipRep;
        private IRepository<Customer> _customerRep;

        public FriendShipService(IRepository<FriendShip> friendShipRep, IRepository<Customer> customerRep)
        {
            _friendShipRep = friendShipRep;
            _customerRep = customerRep;
        }

        public void Detroy(int ownerId, int friendId)
        {
            throw new System.NotImplementedException();
        }

        public IList<Customer> Follows(int customerId)
        {
            var query = from r in _friendShipRep.Table
                        join cust in _customerRep.Table 
                        on r.FriendId equals  cust.Id
                        where r.OwnerId== customerId
                        select cust;

            var result = query.Distinct().ToList();

            if (result != null)
            {
                return CustomerMapper.ToCustomers(result);
            }
            return null;
        }

        public IList<Customer> Fans(int customerId)
        {
            var query = from r in _friendShipRep.Table
                        join cust in _customerRep.Table
                        on r.OwnerId equals cust.Id
                        where r.FriendId==customerId 
                        select cust;

            var result = query.Distinct().ToList();

            if (result != null)
            {
                return CustomerMapper.ToCustomers(result);
            }
            return null;
        }

        //public IList<Customer> Friends(int customerId)
        //{
        //    var query = from r in _friendShipRep.Table
        //                join cust in _customerRep.Table 
        //                on r.FriendId equals  cust.Id
        //                where r.OwnerId == customerId
        //                select cust;

        //    var result = query.ToList();

        //    if (result != null)
        //    {
        //        return CustomerMapper.ToCustomers(result);
        //    }
        //    return null;


        //}

        public void CreateFriendShip(int ownerId, int toId)
        {
            var friendship = new FriendShip()
                {
                    OwnerId = ownerId,
                    FriendId = toId
                };
            _friendShipRep.Insert(friendship);
        }

        public bool IsFriend(int ownerId, int friendId)
        {
            var query = from f in _friendShipRep.Table
                        where f.OwnerId == ownerId
                              && f.FriendId == friendId
                        select f;
            return query.Any();

        }
    }
}